//
//  ComEduTopView.m
//  fp
//
//  Created by xingshuai on 2020/11/14.
//

#import "ComEduTopView.h"

#import "EduTypeCollectionViewCell.h"
static NSString *identifier = @"XAComEduTopCollectionCellID";

@interface ComEduTopView () <UICollectionViewDelegate, UICollectionViewDataSource>
// 数据展示
@property (nonatomic, strong) UICollectionView *collectionTop;
// 数据展示
@property (nonatomic, strong) UICollectionView *collectionBottom;
// 数据展示
@property (nonatomic, strong) NSMutableArray *topTextWidthArr;
// 数据展示
@property (nonatomic, strong) NSMutableArray *botTextWidthArr;

@end

@implementation ComEduTopView

-(NSMutableArray *)topTextWidthArr {
    if (!_topTextWidthArr) {
        _topTextWidthArr = [[NSMutableArray alloc]init];
    }
    return _topTextWidthArr;
}

-(NSMutableArray *)botTextWidthArr {
    if (!_botTextWidthArr) {
        _botTextWidthArr = [[NSMutableArray alloc]init];
    }
    return _botTextWidthArr;
}

// 初始化collection
- (UICollectionView *)collectionTop {
    if (!_collectionTop) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionTop = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collectionTop registerClass:[EduTypeCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        _collectionTop.showsHorizontalScrollIndicator = NO;
        _collectionTop.dataSource = self;
        _collectionTop.delegate = self;
        _collectionTop.tag = 100;
        _collectionTop.backgroundColor = [UIColor clearColor];
        _collectionTop.showsVerticalScrollIndicator = NO;
    }
    return _collectionTop;
}

// 初始化collection
- (UICollectionView *)collectionBottom {
    if (!_collectionBottom) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionBottom = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collectionBottom registerClass:[EduTypeCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        _collectionBottom.showsHorizontalScrollIndicator = NO;
        _collectionBottom.dataSource = self;
        _collectionBottom.delegate = self;
        _collectionBottom.tag = 101;
        _collectionBottom.backgroundColor = [UIColor clearColor];
        _collectionBottom.showsVerticalScrollIndicator = NO;
    }
    return _collectionBottom;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.collectionTop];
        [self addSubview:self.collectionBottom];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self.collectionTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_offset(60);
    }];
    
    [self.collectionBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionTop.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_offset(60);
    }];
}

#pragma mark - UICollectionViewDelegateFlowLayout
// 返回cell的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 100) {
        CGFloat wid = [[self.topTextWidthArr objectAtIndex:indexPath.row] floatValue];
        return CGSizeMake(wid, 60);
    } else {
        CGFloat wid = [[self.botTextWidthArr objectAtIndex:indexPath.row] floatValue];
        return CGSizeMake(wid, 60);
    }
}

#pragma mark - deleDate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return collectionView.tag == 100 ? self.topList.count : self.bottomList.count;
}

// cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    EduTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (collectionView.tag == 100) {
        if (self.topList.count > indexPath.row) {
            [cell creatCellModel:[self.topList objectAtIndex:indexPath.row] collectionTag:collectionView.tag];
        }
    } else {
        if (self.bottomList.count > indexPath.row) {
            [cell creatCellModel:[self.bottomList objectAtIndex:indexPath.row] collectionTag:collectionView.tag];
        }
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView.tag == 100) {
        [self.topList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            EduCellModel *model = obj;
            if (idx == indexPath.row) {
                model.isSelect = YES;
            } else {
                model.isSelect = NO;
            }
        }];
        
        [self.collectionTop reloadData];
    } else {
        [self.bottomList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            EduCellModel *model = obj;
            if (idx == indexPath.row) {
                model.isSelect = YES;
            } else {
                model.isSelect = NO;
            }
        }];
        
        [self.collectionBottom reloadData];
        
        [self.collectionBottom scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}

-(void)setTopList:(NSArray *)topList {
    _topList = topList;
    // 获取每个分段文字宽度
    [self.topTextWidthArr removeAllObjects];
    CGFloat wid = 0;
    for (EduCellModel *model in topList) {
        wid = [self getWidthWithText:model.labelStr height:35 font:[UIFont pingFangSCWithWeight:FontWeightStyleMedium size:20]];
        [self.topTextWidthArr addObject:[NSString stringWithFormat:@"%.lf",wid]];
    }
    
    [self.collectionTop reloadData];
}

-(void)setBottomList:(NSArray *)bottomList {
    _bottomList = bottomList;
    // 获取每个分段文字宽度
    [self.botTextWidthArr removeAllObjects];
    CGFloat wid = 0;
    for (EduCellModel *model in bottomList) {
        wid = [self getWidthWithText:model.labelStr height:35 font:[UIFont pingFangSCWithWeight:FontWeightStyleMedium size:20]];
        [self.botTextWidthArr addObject:[NSString stringWithFormat:@"%.lf",wid]];
    }
    
    [self.collectionBottom reloadData];
}

// 获取文字宽度
-(CGFloat)getWidthWithText:(NSString*)text height:(CGFloat)height font:(UIFont *)font{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    return rect.size.width+40;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
